<template>
  <Panel title="商品添加">
    <div slot="container">
      <!-- 商品名称 -->
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="商品名称">
          <el-input
            v-model="form.name"
            class="nameInput"
            placeholder="商品名称"
          ></el-input>
        </el-form-item>

        <!-- 商品分类 -->
        <el-form-item label="商品分类">
          <el-select v-model="form.category" placeholder="请选择商品分类">
            <el-option
              v-for="v in category"
              :key="v"
              :label="v"
              :value="v"
            ></el-option>
          </el-select>
        </el-form-item>

        <!-- 商品价格 -->
        <el-form-item label="商品价格">
          <el-input-number
            v-model="form.price"
            @change="handleChange"
            :min="0"
            :precision="2"
            :step="0.1"
          ></el-input-number>
        </el-form-item>

        <!--商品图片 -->
        <el-form-item label="商品图片">
          <el-upload
            class="avatar-uploader"
            action="http://sell.h5.itsource.cn:8087/goods/goods_img_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.imgUrl" :src="form.imgUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <!--商品描述 -->
        <el-form-item label="商品描述">
          <el-input
            type="textarea"
            v-model="form.goodsDesc"
            class="item-input"
          ></el-input>
        </el-form-item>

        <!--添加商品按钮 -->
        <el-form-item>
          <el-button type="primary" @click="onSubmit">添加商品</el-button>
        </el-form-item>
      </el-form>
    </div>
  </Panel>
</template>

<script>
import Panel from "@/components/Panel.vue";

//接口
import { reqCateGories, reqAdd } from "@/api/goods";

export default {
  components: {
    Panel,
  },
  data() {
    return {
      form: {
        name: "", //商品名称
        category: "", //商品品类
        price: "", //商品价格
        imgUrl: "", //商品图片名称
        goodsDesc: "", //商品描叙
      },

      //商品类别
      category: [],
    };
  },

  //生命周期渲染商品获取分类
  created() {
    reqCateGories().then((res) => {
      this.category = res.data;
    });
  },

  //方法
  methods: {
    // 添加商品按钮
    async onSubmit() {
      let res = await reqAdd(this.form);
      console.log(res);
      //判定
      if (res.data.code == 0) {
        this.$router.push("/goods/goodslist");
      }
    },

    // 商品价格
    handleChange() {},

    //商品图片
    handleAvatarSuccess(res) {
      // console.log(res);
      this.form.imgUrl = res.imgUrl;
    },

    //商品图片格式判断
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2; //商品图片大小判断
      if (!isJPG && !isPNG) {
        this.$message.error("上传头像图片只能是 JPG,PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return (isJPG || isPNG) && isLt2M;
    },
  },
};
</script>

<style lang="less" scoped>
.nameInput {
  width: 400px;
}
.item-input {
  width: 400px;
}
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>